package com.xms.plugin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.core.data.Entity;
import com.xms.core.enums.OperationType;
import com.xms.core.exception.XmsException;
import com.xms.datacore.service.CreaterService;
import com.xms.plugin.core.PluginType;
import com.xms.plugin.entity.EntityPlugin;
import com.xms.security.identity.ICurrentUser;
import com.xms.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.UUID;

/**
 * <p>
 * EntitypluginCreaterService
 * </p>
 *
 * @author migo
 * @since 2021/10/15
 */
@CacheConfig(cacheNames = "entityplugin", keyGenerator = "CacheKeyGenerator")
@Service
public class EntityPluginCreaterService extends CreaterService<EntityPlugin> implements IEntityPluginCreaterService {
    private ICurrentUser _currentUser;

    @Autowired
    public EntityPluginCreaterService(ICurrentUser currentUser) {
        _currentUser = currentUser;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean create(EntityPlugin entity) throws SQLException, XmsException {
        return super.save(entity);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean createMany(Collection<EntityPlugin> entityList) throws SQLException, XmsException {
        for (EntityPlugin entity : entityList) {
            super.save(entity);
        }
        return true;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean create(Class pluginClass, UUID entityId, OperationType operationType, int order, boolean visibled) throws XmsException, SQLException {
        //先判断是否存在
        if(super.count(new LambdaQueryWrapper<EntityPlugin>().eq(EntityPlugin::getEntityid, entityId)
        .eq(EntityPlugin::getEventcode, operationType.value())
        .eq(EntityPlugin::getClassname, pluginClass.getName())) > 0){
            return false;
        }
        EntityPlugin entityPlugin = new EntityPlugin();
        entityPlugin.setEntityid(entityId);
        entityPlugin.setCreatedby(_currentUser.getUser().SystemUserId);
        entityPlugin.setEntitypluginid(UUIDUtil.get());
        entityPlugin.setCreatedon(LocalDateTime.now());
        entityPlugin.setOrganizationid(_currentUser.getUser().OrganizationId);
        entityPlugin.setIsvisibled(visibled);
        entityPlugin.setEventcode(operationType.value());
        entityPlugin.setProcessorder(order);
        entityPlugin.setStatecode(true);
        entityPlugin.setTypecode(PluginType.Entity.value());
        entityPlugin.setAssemblyname("");
        entityPlugin.setClassname(pluginClass.getName());
        return this.create(entityPlugin);
    }
}
